package com.example.springboot.service.impl;

import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.Page;
import com.example.springboot.service.BlockedWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlockedWordServiceImpl implements BlockedWordService {

    @Autowired
    private BlockedWordMapper blockedWordMapper;

    @Override
    public void addBlockedWord(BlockedWord blockedWord) {
        try {
            blockedWordMapper.addBlockedWord(blockedWord);
        } catch (Exception e) {
            throw new ServiceException("添加屏蔽词失败");
        }
    }

    @Override
    public void updateBlockedWord(BlockedWord blockedWord) {
        try {
            blockedWordMapper.updateBlockedWord(blockedWord);
        } catch (Exception e) {
            throw new ServiceException("更新失败");
        }
    }

    @Override
    public void deleteBlockedWordById(int id) {
        try {
            blockedWordMapper.deleteBlockedWordById(id);
        } catch (Exception e) {
            throw new ServiceException("删除失败");
        }
    }

    @Override
    public List<BlockedWord> getAllBlockedWords() {
        try {
            return blockedWordMapper.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取屏蔽词列表失败");
        }
    }

    @Override
    public Page<BlockedWord> getByManyConditionInPage(BlockedWord blockedWord, Integer currentPage, Integer pageSize) {
        try {
            Integer startRow = null;
            if (currentPage != null || pageSize != null) {
                startRow = (currentPage - 1) * pageSize;
            }
            List<BlockedWord> blockedWords = blockedWordMapper.getByManyConditionInPage(blockedWord, startRow, pageSize);
            startRow = null;
            pageSize = null;
            List<BlockedWord> blockedWordTotal = blockedWordMapper.getByManyConditionInPage(blockedWord, startRow, pageSize);
            Integer total = blockedWordTotal.size();
            Page<BlockedWord> page = new Page<BlockedWord>();
            page.setTotal(total);
            page.setList(blockedWords);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("获取屏蔽词列表失败");
        }
    }

    @Override
    public void deleteBlockedWords(List<BlockedWord> blockedWords) {
        try {
            for (BlockedWord blockedWord : blockedWords) {
                blockedWordMapper.deleteBlockedWordById(blockedWord.getId());
            }
        } catch (Exception e) {
            throw new ServiceException("删除失败！");
        }
    }
}
